package cloudLibrary;

import java.io.IOException;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/*
 * this class registers a user in the Users table of the database. 
 */

@SuppressWarnings("serial")
public class UserRegistrationServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		if (user == null)
			resp.sendRedirect(userService.createLoginURL(req.getRequestURI())); // demand
																				// user
																				// to
																				// be
																				// logged
																				// in.

		// =====Registering a user in the database====

		Users u = new Users(user.getEmail(), user.getNickname(), 2);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			if (!isRegisted(user.getEmail())) {
				pm.makePersistent(u);
				resp.sendRedirect("?friendList&msg=registered");
			} else
				resp.getWriter().println(
						"This email is already registered in the system.");
		} finally {
			pm.close();
		}

	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		

		if (user == null)
			resp.sendRedirect(userService.createLoginURL(req.getRequestURI())); // demand
																				// user
																				// to
																				// be
																				// logged
																				// in.
		String email ="";
		String name="";
		long tel=0;
		try{
			email = req.getParameter("email");
			name = req.getParameter("name");
			tel = Long.parseLong(req.getParameter("tel"));
		}catch (Exception e) {
			resp.sendRedirect("?page=newFriend&error=fields");
			
		}
		
		if (email == null || name == null || tel == 0) {
			resp.sendRedirect("?page=newFriend&error=fields");
		} else {
			Users u = new Users(email, name, tel);
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try {
				if (!isRegisted(email)) {
					pm.makePersistent(u);
					resp.sendRedirect("?page=friendsList&msg=registered");
				} else
				resp.sendRedirect("?page=newFriend&error=alreadyregistered");
			} finally {
				pm.close();
			}
		}
	}

	public boolean isRegisted(String email) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Users.class);
		query.setFilter("email == emailParam");
		query.declareParameters("String emailParam");

		try {
			List<Users> results = (List<Users>) query.execute(email);
			if (!results.isEmpty()) {
				return true;
			} else {
				return false;
			}
		} finally {
			query.closeAll();
		}

	}

}
